package reflex_and_annotation.d_annotation;

import reflex_and_annotation.d_annotation.annotation.Column;
import reflex_and_annotation.d_annotation.annotation.Table;

import java.lang.reflect.Field;

/**
 * @Author WLJ
 * @Description //TODO 通过反射解析注解
 * @Date 10:26 2023/8/31
 * @Param
 * @return
 **/
public class Test {
    public static void main(String[] args) throws ClassNotFoundException {
        Class clazz = Class.forName("reflex_and_annotation.d_annotation.entity.Student");
        if (clazz.getAnnotation(Table.class) != null) {
            // 获取表注解
            Table tableAnnotation = (Table) clazz.getAnnotation(Table.class);
            System.out.println("表注解，表名：【" + tableAnnotation.tableName() + "】，值：【" + tableAnnotation.value() + "】");
        }
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            Column column = field.getAnnotation(Column.class);
            if (column != null) {
                System.out.println("字段注解，字段名：【" + column.name() + "】，是否是主键：【" + column.isId() + "】" +
                        "，是否可为空：【" + column.isBlack() + "】，最大长度：【" + column.maxLen() + "】，最小长度：【" + column.minLen() + "】");
            }
        }
    }
}
